package main

import "fmt"

/**
Implement strStr().

Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

Example 1:

Input: haystack = "hello", needle = "ll"
Output: 2
Example 2:

Input: haystack = "aaaaa", needle = "bba"
Output: -1

实现strstr,找出字符串B在字符串A的位置并返回
*/

func strStr(haystack string, needle string) int {
	m := len(haystack)
	n := len(needle)

	//拿needle第一个字符与haystack字符匹配，匹配到的话对needle之后的字符做与haystack做匹配  m比n大 所以遍历haystack的最后一个字符位置应该是m-n
	for i := 0; i <= (m - n); i++ {
		flag := true
		for j := 0; j < n; j++ {
			if haystack[i+j] != needle[j] {
				flag = false
				break
			}
		}

		if flag {
			return i
		}
	}

	return -1
}

func main() {
	fmt.Print(strStr("mississippi", "issippi"))
}
